package com.hk.webapp.controller.admin;

import com.hk.webapp.annotations.AllowAnonymous;
import com.hk.webapp.annotations.SkipPermission;
import com.hk.webapp.annotations.validation.groupa.GroupA;
import com.hk.webapp.bo.UserBo;
import com.hk.webapp.res_config.JwtAudience;
import com.hk.webapp.service.UserService;
import com.hk.webapp.utils.JwtUtil;
import com.hk.webapp.utils.encryptions.DESUtils;
import com.hk.webapp.vo.Login02Vo;
import com.hk.webapp.vo.LoginVo;
import com.hk.webapp.vo.ResultVo;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@SkipPermission
public class LoginController extends AdminBaseController {

    @Resource
    private UserService userService;
    @Resource
    private JwtAudience audience;

    @AllowAnonymous
    @PostMapping("/Login/Index")
    public ResultVo login(@RequestBody @Validated(GroupA.class) LoginVo loginVo, BindingResult result) throws Exception {
        if (result.hasErrors()) {
            List<ObjectError> errors = result.getAllErrors();
            ObjectError error = errors.get(0);
            return ResultVo.createByError(error.getDefaultMessage());
        }
        try {
            String vcode = loginVo.getVcode().toUpperCase();
            String encode = loginVo.getEncode();
            String decryptorCode = DESUtils.decryptor(encode).toUpperCase();
            if (!vcode.equals(decryptorCode)) {
                return ResultVo.createByError("您输入的验证码有误!");
            }
        } catch (Exception e) {
            return ResultVo.createByError("您输入的验证码有误!");
        }
        //去数据库中校验是否有改用户
        //并且验证是否登录成功
        ResultVo usableUserVo = userService.findUsableUser(loginVo);
        if (!usableUserVo.isSuccess()) {
            return usableUserVo;
        }
        UserBo userBo = (UserBo) usableUserVo.getData();
        String jwtToken = JwtUtil.generateToken(audience, userBo.getUserId(), userBo.getUsername(), true);
        return ResultVo.createBySuccessData(jwtToken);
    }

    @AllowAnonymous
    @PostMapping("/Login/Login02")
     public   ResultVo  login02(@RequestBody @Valid Login02Vo vo ,BindingResult result)  throws  Exception
     {
         if (result.hasErrors())
         {
             List<ObjectError>errors=result.getAllErrors();
             ObjectError error=errors.get(0);
             return      ResultVo.createByError(error.getDefaultMessage());
          }
         //验证用户
         //校验是否有该用户
         //校验是否登录成功
            try {
                ResultVo usableUserVo = userService.findUsableUser02(vo);
                if (!usableUserVo.isSuccess()) {
                    return usableUserVo;
                }
                UserBo userBo = (UserBo) usableUserVo.getData();
                String jwtToken = JwtUtil.generateToken(audience, userBo.getUserId(), userBo.getUsername(), true);
                return ResultVo.createBySuccessData(jwtToken);
            }
            catch (Exception e)
            {
                return ResultVo.createByError("登录失败!");
            }
         //return  ResultVo.createBySuccessData("success");
     }

}
